package controller;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import service.MplanService;


import entity.Mplan;
import entity.Plan;
@Controller
@RequestMapping("/mplan/")
public class MplanController {
	@Resource
	private MplanService ms;
	
	@RequestMapping("add")
	public String add(Mplan mplan,String[] key,String[] va,Model model){
	String meg=null;
		if(!ms.add(mplan, key, va)){
			meg="添加失败！";
		};
		meg="添加成功！";
		model.addAttribute("meg", meg);
		return "main";
	}
	@RequestMapping("one/{pid}")
	public String one(@PathVariable("pid") int pid,String name,Model model){
		Mplan mp=ms.getone(pid);
		if(mp==null){
			try {
				Plan plan=new Plan();
				plan.setName(new String(name.getBytes("iso-8859-1"),"utf-8"));
				plan.setId(pid);
				model.addAttribute("plan", plan);
				model.addAttribute("meg", "该计划没有添加明细，请添加明细！");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return "mplanadd";
		}
		String[] va=mp.getVa().split("/");
		String[] key=mp.getKe().split("/");
		model.addAttribute("va", va);
		model.addAttribute("key", key);
		model.addAttribute("mp", mp);
		return "mplanshow";
	}
	
	@RequestMapping(value="update")
	public String updatesave(Mplan mp,String[] key,String[] va,Model model){
		String meg=null;
		if(ms.update(mp, key, va)){
			meg="更新成功！";
		}else{
			meg="更新失败！";
		}
		model.addAttribute("meg", meg);
		return "main";
	}
	

}
